/**
 * Project Name:meatball-admin
 * File Name:MenuServiceImpl.java
 * Package Name:com.meatball.base.system.menu.service.impl
 * Date:2018年1月13日下午2:14:46
 * Copyright (c) 2018, zhang.xiangyu@foxmail.com All Rights Reserved.
*/
package com.meatball.base.system.menu.service.impl;

import java.util.*;

import javax.annotation.Resource;

import com.meatball.base.system.role.dao.SysRoleMapper;
import com.meatball.base.system.role.model.SysRole;
import com.meatball.base.system.user.model.SysUser;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;

import com.meatball.base.system.menu.dao.SysMenuMapper;
import com.meatball.base.system.menu.model.SysMenu;
import com.meatball.base.system.menu.service.MenuService;
import com.meatball.vo.ResultMsg;

/**   
 * @Title: MenuServiceImpl.java 
 * @Package com.meatball.base.system.menu.service.impl
 * @Description: TODO(用一句话描述该文件做什么) 
 * @author 張翔宇  
 * @date 2018年1月13日 下午2:14:46 
 * @version V1.0   
 */
@Service
public class MenuServiceImpl implements MenuService {
	@Resource
	private SysMenuMapper mapper;
	@Resource
	private SysRoleMapper sysRoleMapper;
	
	@Override
	public List<SysMenu> selectAllTreeMenu() {
		List<SysMenu> menus = mapper.selectAllTreeMenu();
		this.deleteNullChildren(menus);
		return menus;
	}
	
	@Override
	public SysMenu info(Long id) {
		return mapper.selectByPrimaryKey(id);
	}
	
	@Override
	public ResultMsg add(SysMenu record) {
		if(record.getType() == 0) {
			record.setPid((long) 0);
		} else {
			if(record.getHierarchy().intValue() == 1) {
				record.setPid(record.getId());
			}
		}
		record.setId(null);
		record.setCreateTime(new Date());
		mapper.insertSelective(record);
		return new ResultMsg(200, "新增成功");
	}
	
	@Override
	public ResultMsg updata(SysMenu record) {
		mapper.updateByPrimaryKeySelective(record);
		return new ResultMsg(200, "更新成功");
	}
	
	@Override
	public ResultMsg del(Long id) {
		// 删除菜单
		mapper.deleteByPrimaryKey(id);
		// 删除子菜单
		mapper.deleteByPid(id);
		return new ResultMsg(200, "删除成功");
	}

	@Override
	public ResultMsg getAllDirectoryMenu() {
		SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("sysUser");
		// 获取权限信息
		List<SysRole> roles = (List<SysRole>) SecurityUtils.getSubject().getSession().getAttribute("roles");
		// 获取菜单列表
		// 获取权限字符串
		StringBuffer perms = new StringBuffer();
		roles.forEach(role -> {
			perms.append(role.getRoleSign());
			perms.append(",");
		});
		List<SysMenu> menus = mapper.selectAllDirectoryMenu();
		menus = this.deleteNotPermisMenu(menus, perms.toString());
		return new ResultMsg(200, menus);
	}

	@Override
	public ResultMsg table(SysMenu record) {
		// PageInfo<SysMenu> page = PageHelper.startPage(record.getOffset(), record.getLimit()).doSelectPageInfo(() -> mapper.findPageList(record));
		List<SysMenu> menus = mapper.selectAllMenu();
		return new ResultMsg(200, menus);
	}

	/**
	 * @Title: deleteNullChildren
	 * @Description: TODO(递归清除空children)
	 * @param menus
	 * @return List<SysMenu>    返回类型
	 */
	private List<SysMenu> deleteNullChildren(List<SysMenu> menus) {
		for(SysMenu info : menus) {
			if(info.getChildren().isEmpty()) {
				info.setChildren(null);
			} else {
				this.deleteNullChildren(info.getChildren());
			}
		}
		return menus;
	}

	/**
	 * @Title: deleteNullChildren
	 * @Description: TODO(递归清除空children)
	 * @param menus
	 * @return List<SysMenu>    返回类型
	 */
	private List<SysMenu> deleteNotPermisMenu(List<SysMenu> menus, String paermis) {
		Iterator<SysMenu> iterator = menus.iterator();
		while(iterator.hasNext()) {
			SysMenu menu = iterator.next();
			if(!paermis.contains(menu.getPerms())) {
				iterator.remove();
			} else {
				if(!menu.getChildren().isEmpty()) {
					this.deleteNotPermisMenu(menu.getChildren(), paermis);
				}
			}
		}
		return menus;
	}
}
